package com.ingenio.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ingenio.business.Categoria;
import com.ingenio.business.Rubro;
import com.ingenio.business.SubRubro;
import com.ingenio.dao.CategoriaAdapter;
import com.ingenio.dao.RubroAdapter;
import com.ingenio.dao.SubRubroAdapter;

/**
 * Servlet implementation class ABMRubroServlet
 */
@WebServlet("/RubroServlet")
public class ABMRubroServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ABMRubroServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer idRubro = null;
		RubroAdapter ra = new RubroAdapter();
		Rubro r = null;
		CategoriaAdapter ca = null;
		Categoria c = null;
		if("delete".equals(request.getParameter("type"))){
			try {
				Integer id = Integer.parseInt(request.getParameter("id"));
		    	r = ra.devolverRubro(id);
		    	ra.eliminarRubro(r);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else if("deleteSubRubro".equals(request.getParameter("type"))){
			Integer id = Integer.parseInt(request.getParameter("id"));
			SubRubroAdapter sra = new SubRubroAdapter();
			SubRubro subRubro;
			try {
				subRubro = sra.devolverSubRubro(id);
				sra.eliminarSubRubro(subRubro);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}else if("edit".equals(request.getParameter("type"))){
			if(true){
				String descripcion_rubro = request.getParameter("descripcion_rubro");
				Integer id_rubro = Integer.parseInt(request.getParameter("id_rubro"));
				Integer id_categoria = Integer.parseInt(request.getParameter("id_categoria"));
				String descSubrubros[] = request.getParameterValues("descripcion_subrubro");
				String idSubrubros[] = request.getParameterValues("id_subrubro");
				try {
					ca = new CategoriaAdapter();
					c = ca.devolverCategoria(id_categoria);
					r = ra.devolverRubro(id_rubro);
					List<SubRubro> subrubros = new ArrayList<>();
					SubRubro subRubro = null;
					Integer id = null;
					for (int i = 0; i < descSubrubros.length-1; i ++) {
						id = Integer.parseInt(idSubrubros[i]);
						subRubro = new SubRubro(id, descSubrubros[i], r, true);
						subrubros.add(subRubro);
					}
					r.setSubrubros(subrubros);
					r.setDescRubro(descripcion_rubro);
					r.setCategoria(c);
					ra.modificarRubro(r);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} else if("add".equals(request.getParameter("type"))){
			String descripcion_rubro = request.getParameter("descripcion_rubro");
			Integer id_categoria = Integer.parseInt(request.getParameter("id_categoria"));
			String descSubrubros[] = request.getParameterValues("descripcion_subrubro");
			List<SubRubro> subrubros = new ArrayList<>();
			SubRubro subrubro = null;
			try {
				ca = new CategoriaAdapter();
				c = ca.devolverCategoria(id_categoria);
				for (String sr : descSubrubros) {
					if(sr.trim() != "") {
						subrubro = new SubRubro(null, sr, null, true);
						subrubros.add(subrubro);
					}
				}
				r = new Rubro(idRubro, descripcion_rubro, c, true);
				r.setSubrubros(subrubros);
				ra.agregarRubro(r);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		response.sendRedirect("rubros/listado_rubros.jsp");
	}
}
